package study.db.postgresql.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import study.db.postgresql.mapper.examenrolluser.ExamEnrollUserEO;
import study.db.postgresql.mapper.examenrolluserext.ExamEnrollUserExtEO;

import java.util.List;

@Slf4j
@Component
public class DataRefactorHelper {

    public static List<ExamEnrollUserExtEO> splitBySubject(ExamEnrollUserEO userEO) {
        String subjectCodes = userEO.getSubjectCodes();
        List<String> codeLt = StrUtil.split(subjectCodes, ",");
        List<String> nameLt = StrUtil.split(userEO.getSubjectNames(), ",");

        List<ExamEnrollUserExtEO> extEOLt = CollectionUtil.newArrayList();
        for (int i = 0; i < codeLt.size(); i++) {
            String code = CollectionUtil.get(codeLt, i);
            String name = CollectionUtil.get(nameLt, i); //越界时返回 null

            ExamEnrollUserExtEO extEO = ExamEnrollUserExtEO.builder()
                    .enrollId(userEO.getId())
                    .subjectCode(code)
                    .subjectName(name)
                    .examRoom(userEO.getExamRoom())
                    .seatNo(userEO.getSeatNo())
                    .build();

            extEOLt.add(extEO);
        }
        return extEOLt;
    }
}
